package com.ztrue.framework.context;

import com.ztrue.framework.Config;
import com.ztrue.framework.resources.ClassPathHandlerDefinitionScanner;
import com.ztrue.common.log.Logger;
import com.ztrue.common.properties.Prop;
import com.ztrue.common.properties.Proper;
import com.ztrue.common.utilities.Assert;

/**
 * 应用程序上下文
 * 通过类路径加载指定资源
 *
 * @author zhangw
 * @date 2016/11/16 16:07
 * @version: 1.0
 */
public class ClassPathApplicationContext extends AbstractApplicationContext {
    private static final Logger logger = Logger.getLogger(ClassPathApplicationContext.class);

    /**
     * 初始化资源
     */
    @Override
    void init() {
        Prop prop = Proper.use(Config.CONFIG_PATH);
        Assert.notNull(prop, "无法获取默认配置文件");
        logger.info("默认配置文件加载成功...");
        String[] packages = prop.get("ztrue.run.scanning.paths").split(SCANSEPARATOR);
        logger.info("扫描处理器...");
        ClassPathHandlerDefinitionScanner classPathHandlerDefinitionScanner = new ClassPathHandlerDefinitionScanner(this.handlerDefinitionRegistry);
        int count = classPathHandlerDefinitionScanner.scan(packages);
        logger.info(String.format("成功加载 %d 个处理器", count));
    }
}